<?php

    class ItemController extends AdminController
    {
        public function init(){
            parent::init();
            $this->menu_parent_selected = 'item';

        }

        /**
        * @return array action filters
        */
        public function filters()
        {
            return array(
                'accessControl', // perform access control for CRUD operations
                'postOnly + delete', // we only allow deletion via POST request
            );
        }

        /**
        * Specifies the access control rules.
        * This method is used by the 'accessControl' filter.
        * @return array access control rules
        */
        public function accessRules()
        {
            return array(
                array('allow',
                    'actions'=>array('view'),
                    'users'=>array('*'),
                ),
                array('allow', // allow authenticated user to perform 'create' and 'update' actions
                    'actions'=>array('create', 'index', 'delete', 'update'),
                    'users'=>array('@'),
                ),
                array('deny',  // deny all users
                    'users'=>array('*'),
                ),
            );
        }

        public function actionIndex()
        {
            $this->menu_child_selected = 'item';
            $this->menu_sub_selected = 'index';

            $model = new Item('search');
            $model->unsetAttributes();  // clear any default values
            if(isset($_GET['Item'])){
                $model->attributes=$_GET['Item'];  
            }


            $criteria=new CDbCriteria;
            $sort = Yii::app()->request->getQuery('Item_sort');
            if(!$sort && $sort != 'id') $criteria->order = 't.id DESC'; 

            $criteria->compare('name',$model->name,true);
            $criteria->compare('name_en',$model->name_en,true);
            $criteria->compare('iron',$model->iron);
            $criteria->compare('laundry_dry',$model->laundry_dry);
            $criteria->compare('`group`',$model->group);
            $criteria->compare('unit',$model->unit);

            $dataProvider = new CActiveDataProvider($model, array(
                'criteria'=>$criteria,
                'pagination'=>array(
                    'pageSize'=>100,
                ),
            ));

            $this->render('index',array(
                'model'         =>  $model,
                'dataProvider'         =>  $dataProvider,
            ));
        }

        public function actionCreate()
        {   
            $this->menu_child_selected = 'item_create';
            $this->menu_sub_selected = 'create';

            $model=new Item('create');

            if(isset($_POST['ajax']) && $_POST['ajax']==='item-form'){
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            if(isset($_POST['Item'])) {
                $model->attributes=$_POST['Item'];
                $model->index = 0;
                $model->setIsNewRecord(TRUE); 
                if($model->save()){
                    Yii::app()->user->setFlash('success', '<strong>Thành công!</strong> Giá đã được thêm. '. CHtml::link('Sửa '.$model->name, $model->getAdminUrl('update')));
                    $this->refresh();
                }    
            }


            $this->pageTitle = $this->t('Thêm giá').' | '.$this->siteName;
            $this->render('create', compact('model'));
        }

        public function actionUpdate($id)
        {
            $this->menu_child_selected = 'item_create';
            $this->menu_sub_selected = 'create';


            $model=Item::model()->findByPk($id);
            if($model===null) throw new CHttpException(404,'The requested page does not exist.');


            if(isset($_POST['ajax']) && $_POST['ajax']==='item-form'){
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            if(isset($_POST['Item'])) {
                $model->attributes=$_POST['Item'];

                if($model->save()){
                    Yii::app()->user->setFlash('success', '<strong>Thành công!</strong> Giá đã được sửa thành công');
                    $this->refresh();
                }    
            }


            $this->pageTitle = $this->t('Thêm giá').' | '.$this->siteName;
            $this->render('update', compact('model'));
        }

        public function actionDelete($id)
        {
            if(Yii::app()->request->isPostRequest)
            {
                $model=Item::model()->findByPk($id);
                if($model===null) throw new CHttpException(404,'The requested page does not exist.');
                
                $model->delete();

                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                if(!isset($_GET['ajax']))
                    $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
            else
                throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
        }
    }
